package com.example.learnnacos.tasks;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tech.powerjob.client.PowerJobClient;
import tech.powerjob.common.response.ResultDTO;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;

/**
 *  通过http://localhost:8081/powerjob/runjob触发job；prower-job server 触发 process方法
 */
@Slf4j
@Service
public class ApiJobProcessor implements BasicProcessor {
    @Value("${spring.application.name}")
    private String appName;

    private Long jobId = 2L;

    public void runJob() {
        PowerJobClient client = new PowerJobClient("192.168.31.113:7700", appName, "server-b");
        ResultDTO<Long> triggerResult = client.runJob(jobId);
        log.info("resultDTO:{}", triggerResult);
    }

    @Override
    public ProcessResult process(TaskContext taskContext) throws Exception {
        log.info("process taskContext:{}", taskContext);
        return new ProcessResult(true, "result is true");
    }
}
